#! /bin/sh

source /etc/rc.d/common

DRBDADM="/sbin/drbdadm"
DEV_TIMEOUT=10
PROC_DRBD="/proc/drbd"

check_args ${@}

# Suppress version mismatch warnings
export DRBD_DONT_WARN_ON_VERSION_MISMATCH=1

start() {
    /bin/echo "Setting DRBD parameters..."
    ${DRBDADM} adjust all

    /bin/echo "Waiting for device creation..."
    for i in `${DRBDADM} sh-resources`; do
        for j in `${DRBDADM} sh-dev ${i}`; do
            dev_timeout_local=${DEV_TIMEOUT}
            while [ ! -e ${j} ] && [ ${dev_timeout_local} -gt 0 ] ; do
                /bin/sleep 1
                dev_timeout_local=$(( ${dev_timeout_local}-1 ))
            done
        done
    done

    /bin/echo "Waiting for connection..."
    ${DRBDADM} wait-con-int
}

stop() {
    /bin/echo "Stopping all DRBD resources..."
    if ! grep -q '^/dev/drbd' /proc/mounts &> /dev/null; then
        if [ -e ${PROC_DRBD} ]; then
            ${DRBDADM} down all
        fi
    else
        /bin/echo "DRBD devices mounted; please unmount them before trying to stop DRBD!"
    fi
}

status() {
    # We don't know anything
    exit ${UNKNOWN}
}

# Perform specified action
${1}
